class HTTP {
    request({
        url,
        data = {},
        method = 'POST'
    }) {
        return new Promise((resolve, reject) => {
            this._request(url, resolve, reject, data, method)
        })
    }

    _request(url, resolve, reject, data = {}, method = 'POST') {
        var urls = urlapi.host + urlapi.hostapi + url
        axios({
            method: method,
            url: urls,
            data: data,
            transformRequest: [function(data) {
                let ret = ''
                for (let it in data) {
                    ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                }
                var str = ret;
                str = (str.substring(str.length - 1) == '&') ? str.substring(0, str.length - 1) : str;
                return str
            }],
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).then(function(res) {
            if (res.data.code == 0) {
                Toast(res.data.msg);
            }
            if (res.data.code == 401) {
                Toast(res.data.msg);
            }
            resolve(res.data)

        }).catch(function(error) {
            console.log(error)
            // if (error.response && error.response.status === 401) {
            //     Toast('登录信息失效，请重新登录')
            //     localStorage.removeItem('userinfo')
            //     router.replace('login')
            // } else {
            //     Message.error(error.message);
            //     return Promise.reject(error.message);
            // }
        })
    }

    _showerror(msg) {
        alert(msg);
    }
}
const urlapi = {
    host: 'http://wy.bii.cn',
    hostapi: '/api/',
    sms: "sms/send",
    register: "user/register",
    login: "user/login",
    resetpwd: "user/resetpwd",
    logout: "user/logout",
    avatarUpload: "user/avatarUpload",
    verify: "user/verify",
    bindbank: "user/bindbank",
    bindaddress: "user/bindaddress",
    refresh: "user/refresh",
    getuserinfo: "user/getuserinfo",
    experiencelist: "share/experiencelist",
    category: "share/category",
    experiencedetail: "share/experiencedetail",
    experiencesubmit: "share/experiencesubmit",
    uploadthumb: "share/uploadthumb",
    about: "index/about",
    firstcategory: "supply/firstcategory",
    secondcategory: "supply/secondcategory",
    thirdcategory: "supply/thirdcategory",
}

class userModel extends HTTP {
    //获取短信
    getSms(mobile, event = '') {
        return this.request({
            url: urlapi.sms,
            data: {
                mobile: mobile,
                event: event
            }
        })
    }

    // 注册
    getRegister(mobile, captcha, password) {
        return this.request({
            url: urlapi.register,
            data: {
                mobile: mobile,
                captcha: captcha,
                password: password
            }
        })
    }

    // 登录
    getLogin(account, password) {
        return this.request({
            url: urlapi.login,
            data: {
                account: account,
                password: password
            }
        })
    }

    // 重置密码
    getResetpwd(mobile, newpassword, captcha, type = 'mobile') {
        return this.request({
            url: urlapi.resetpwd,
            data: {
                type: type,
                mobile: mobile,
                newpassword: newpassword,
                captcha: captcha,
            }
        })
    }

    // 登出
    getLogout(token) {
        return this.request({
            url: urlapi.logout,
            data: {
                token: token,
            }
        })
    }

    // 更换头像
    getAvatarUpload(token, imgData) {
        return this.request({
            url: urlapi.avatarUpload,
            data: {
                token: token,
                imgData: imgData,
            }
        })
    }

    // 实名认证
    getVerify(token, nickname, shenfenzheng) {
        return this.request({
            url: urlapi.verify,
            data: {
                token: token,
                nickname: nickname,
                shenfenzheng: shenfenzheng,
            }
        })
    }

    // 绑定银行卡
    getBindbank(token, bankaccount) {
        return this.request({
            url: urlapi.bindbank,
            data: {
                token: token,
                bankaccount: bankaccount
            }
        })
    }

    // 地址
    getBindaddress(token, address) {
        return this.request({
            url: urlapi.bindaddress,
            data: {
                token: token,
                address: address
            }
        })
    }

    // 刷新
    geRefresh(token) {
        return this.request({
            url: urlapi.refresh,
            data: {
                token: token
            }
        })
    }
    // 重新获取用户信息
    geRefreshUserinfo(token) {
        return this.request({
            url: urlapi.getuserinfo,
            data: {
                token: token
            }
        })
    }

    // 获取token
    isLogin() {
        var isLogin = false
        var userinfo = JSON.parse(localStorage.getItem('userinfo'))
        if (userinfo !== null) {
            if (userinfo.userinfo.token === "") {
                isLogin = false
            } else {
                isLogin = true
            }
        } else if (userinfo === null) {
            isLogin = false
        }
        return isLogin
    }
    getToken() {
        var token = ''
        var userinfo = JSON.parse(localStorage.getItem('userinfo'))
        if (userinfo !== null) {
            if (userinfo.userinfo.token === "") {
                token = ''
            } else {
                token = userinfo.userinfo.token
            }
        }
        return token
    }

    getUserInfo() {
        var userinfo = JSON.parse(localStorage.getItem('userinfo'))
        return userinfo
    }

    getAbout() {
        return this.request({
            url: urlapi.about
        })
    }


}